package com.xiaodimall.beforesys.controller;

import com.xiaodimall.common.Constants;
import com.xiaodimall.common.ServiceResultEnum;
import com.xiaodimall.beforesys.service.UserService;
import com.xiaodimall.utils.JsonResult;
import com.xiaodimall.utils.Result;
import com.xiaodimall.utils.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author wyc
 * @description
 * @ClassName UserController
 * @date 2022/11/24 15:38
 */
@Api(tags = "前台用户controller")
@Controller
public class UserController {

    //注入业务层接口
    @Resource
    UserService userService;


    /**
     * 发送验证码
     * @author WYC
     * @param email
     * @param session
     * @return: 结果集
     * @date: 2022/11/24 19:58
     */
    @ApiOperation(value = "发送验证码功能")
    @GetMapping("/sendEmail")
    @ResponseBody
    public JsonResult sendEmail(String email, HttpSession session ){
        return userService.sendEmailService(email,session);
    }


    /**
     * 注册功能
     * @author WYC
     * @param loginName
     * @param password
     * @param email
     * @param verifyCode
     * @param httpSession
     * @return: 结果集
     * @date: 2022/11/24 19:59
     */
    @ApiOperation(value = "注册功能")
    @PostMapping("/doRegister")
    @ResponseBody
    public Result register(@RequestParam("loginName") String loginName,
                           @RequestParam("password") String password,
                           @RequestParam("email") String email,
                           @RequestParam("verifyCode") String verifyCode,
                           HttpSession httpSession) {
        //调用业务层实现逐层的接口
        String registerResult = userService.register(loginName, password,email,verifyCode,httpSession);
        //判断
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
            //注册成功
            return ResultGenerator.genSuccessResult();
        }
        //注册失败
        return ResultGenerator.genFailResult(registerResult);
    }



    /**
     * 用户登录
     * @author WYC
     * @param loginName
     * @param password
     * @param verifyCode
     * @param httpSession
     * @return: 结果集
     * @date: 2022/12/1 20:41
     */
    @ApiOperation(value = "登录功能")
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestParam("loginName") String loginName,
                        @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode,
                        HttpSession httpSession) {
        //调用实现登录功能的方法
        String loginResult = userService.login(loginName, password , verifyCode,httpSession);
       //判断
        if (ServiceResultEnum.SUCCESS.getResult().equals(loginResult)) {
            //登录成功
            return ResultGenerator.genSuccessResult();
        }
        //登录失败
        return ResultGenerator.genFailResult(loginResult);
    }



    /**
     * 用户退出
     * @author WYC
     * @param httpSession
     * @return: 返回登录页面
     * @date: 2022/12/1 20:42
     */
    @ApiOperation("用户退出")
    @GetMapping("/logout")
    public String logout(HttpSession httpSession) {
        //删除用户的session域信息
        httpSession.removeAttribute(Constants.MALL_USER_SESSION_KEY);
        return "mall/login";
    }

}

